console.log("preload");
const path = require('path');
const fs = require('fs');

if (!window.csutomCss) {
    const cssPath = path.join(__dirname, 'preload.css');
    window.csutomCss = fs.readFileSync(cssPath);
}

const appid = process.env.APP_ID;
const version = process.env.APP_VERSION || '1.0.0';
const ua_appname = process.env.UA_APP_NAME || 'k';
const buildid = process.env.BUILD_ID || '0';

console.log('ua_appname:', process.env.UA_APP_NAME, ua_appname);
console.log('buildid', process.env.BUILD_ID || '1');

//Mozilla/5.0 (Windows NT 10.0; Win64; x64; Koolearn; kooup/1.0.0; W/55/88) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.106 Electron/4.0.5 Safari/537.36
const ua = navigator.userAgent;
var nua = /\([^\\)]*/.exec(ua);
nua = `${nua}; Koolearn; ${ua_appname}/${version}; ${process.platform === 'win32' ? 'W' : 'M'}/${appid}/${buildid}`;

nua = ua.replace(/\([^\\)]*/, nua);

// Object.defineProperty(navigator, 'userAgent', {
//     get: function() {
//         return nua;
//     }
// });

window.onload = () => {
    var style = document.createElement('style');
    style.setAttribute('type', 'text/css');

    if ('textContent' in style) {
        style.textContent = window.csutomCss;
    } else {
        style.styleSheet.cssText = window.csutomCss;
    }
    document.head.appendChild(style);

    if (/^.+:\/\/www\.eeokoo\.com/.test(location.href)) {
        const header = document.getElementById('header-wrapper');
        header && (header.style = 'display:none');
        const footer = document.getElementById('footer-wrapper');
        footer && (footer.style = 'display:none');
    }

    //window.open = () => window;
    window.open = (url) => {
        url = url || '';
        if (url.startsWith("koolearn://")) {
            window.appex && window.appex.openProtocol(url);
            return window;
        }


        if (url.includes('livebroadcast') && url.includes('enter_roombox_room')) {
            const uRL = new URL(url, window.location.href);
            const invokeUrl = uRL.searchParams.get('invokeUrl');
            if (invokeUrl) {
                window.appex && window.appex.openProtocol(decodeURIComponent(invokeUrl));
                return window;
            }
        }

        if (url) {
            location.href = url;
        }
        return window;
    };
};

require('./appex');